package cn.lix.sc.rpc.impl.common.dto;


import cn.lix.sc.rpc.impl.common.constant.ResponseCode;
import cn.lix.sc.rpc.impl.common.exception.BaseException;

import java.io.Serializable;

public class RpcResponse implements Serializable {

    private static final long serialVersionUID = -5335485972159292631L;

    private String result;

    private String errorMsg;

    private Integer code;

    private Long cost;

    public RpcResponse() {
    }

    public static RpcResponse success(String result) {
        return new RpcResponse(result, null, ResponseCode.SUCCESS, null);
    }

    public static RpcResponse success(String result, Long cost) {
        return new RpcResponse(result, null, ResponseCode.SUCCESS, cost);
    }

    public static RpcResponse error(String errorMsg) {
        return new RpcResponse(null, errorMsg, ResponseCode.FAIL, null);
    }

    public static RpcResponse error(BaseException e) {
        return new RpcResponse(null, e.getMessage(), ResponseCode.FAIL, null);
    }

    public static RpcResponse error(String errorMsg, Long cost) {
        return new RpcResponse(null, errorMsg, ResponseCode.FAIL, cost);
    }

    public static RpcResponse error(Integer code, String errorMsg) {
        return new RpcResponse(null, errorMsg, code, null);
    }

    private RpcResponse(String result, String errorMsg, Integer code, Long cost) {
        this.result = result;
        this.errorMsg = errorMsg;
        this.code = code;
        this.cost = cost;
    }

    public boolean ok(){
        return ResponseCode.SUCCESS.equals(this.code);
    }

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }

    public String getErrorMsg() {
        return errorMsg;
    }

    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public Long getCost() {
        return cost;
    }

    public void setCost(Long cost) {
        this.cost = cost;
    }
}
